package com.zxg.androidbuslearn

import android.app.Activity
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.PersistableBundle
import android.widget.TextView
import android.widget.Toast

class MainActivity : AppCompatActivity() {

    public val TAG = "MainActivity"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // 加载布局，初始化view，只进入一次
        LogUtil.logE(TAG, "onCreate")
        LogUtil.logE(TAG, "Task id == " + taskId)

        val mText = findViewById<TextView>(R.id.mainText);
        mText.setOnClickListener { view ->
            Toast.makeText(this, "hello world!" + view.id, Toast.LENGTH_LONG).show()
            val intent = Intent(this@MainActivity, ThirdActivity::class.java)
            intent.putExtra("data", "main 中 跳转 main")
            startActivity(intent)
        }

        LoggerUtil.e("LoggerUtil - test");
        LogUtil.logE("TAG", "LogUtil - test");

        // 这里简写了getIntent 方法,实际上这里的intent相当于通过getIntent调用获取activity中的intent
        val intent = intent
        var data: String? = intent.getStringExtra("data")
        if (data == null)
            data = "data is null！"
        LogUtil.logE(TAG, data);

        val backIntent = Intent()
        backIntent.putExtra("result", "back from main activity")
        // 设置返回上一个Activity，回传的参数。 默认回传 Activity.RESULT_CANCELED
        setResult(Activity.RESULT_OK, backIntent) // setResult 设置的信息 只在当前 Activity 返回时才回传给上一个Activity。写在任何地方都可以
    }

    override fun onStart() {
        super.onStart()
        LogUtil.logE(TAG, "onStart")
        // 每次打开都会进入。页面已创建，但未添加到前台
    }

    override fun onResume() {
        super.onResume()
        LogUtil.logE(TAG, "onResume")
        // 页面添加到前台，执行在onStart后面
    }

    override fun onPause() {
        super.onPause()
        LogUtil.logE(TAG, "onPause")
        // activity 停止， 离开当前页面，页面还在前台
    }

    override fun onStop() {
        super.onStop()
        LogUtil.logE(TAG, "onStop")
        // 页面不在前台，执行在onPause后面
    }

    override fun onRestart() {
        super.onRestart()
        LogUtil.logE(TAG, "onRestart")
        // activity重新启动，执行后，执行onStart
    }

    override fun onDestroy() {
        super.onDestroy()
        LogUtil.logE(TAG, "onDestroy")
        // activity被销毁
    }

    // activity 异常情况会执行 onPause --> onSaveInstanceState —> onStop --> onCreate -> onStart --> onRestoreInstanceState --> onResume
    // 在系统未经许可销毁activity时一定会调用。例如：屏幕旋转
    override fun onSaveInstanceState(outState: Bundle?) {
        LogUtil.logE(TAG, "onSaveInstanceState")
        super.onSaveInstanceState(outState)
        val data = "save data2"
        outState!!.putString("key", data)
    }
    override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
        LogUtil.logE(TAG, "onRestoreInstanceState")
        super.onRestoreInstanceState(savedInstanceState)
        // onCreate中可能会接收不到onSaveInstanceState传的数据，但这里是一定可以接收到
        val data = savedInstanceState!!.getString("key")
        LogUtil.logE(TAG, data)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        LogUtil.logE(TAG, "onNewIntent: getData==" + intent?.getStringExtra("data"))
    }
}
